using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_ClientRelation_AnnalManage : Member, IRequiresSessionState
{
	public string m_flag = "0";
	private string linkHead = "";
	public string nowTime = DateTime.Now.ToString();
	private string tabMenu = "";
	private string actionTab = "";
	private int annalID = 0;
	private HoWave.Data.Bll.ClientAnnal annalBll;
	protected ScriptManager ScriptManager1;
	protected Label lblManage;
	protected Label lblAddOrEdit;
	protected TextBox txtBeginTime;
	protected TextBox txtEndTime;
	protected TextBox txtSearchClientName;
	protected DropDownList ddlSearchRelationType;
	protected Button SeachBut;
	protected DropDownList ddlDate;
	protected GridView gvManage;
	protected Literal ltrPagePanel;
	protected Button btnMoreDelete;
	protected PlaceHolder phManage;
	protected Label lblClientName;
	protected Label lblRelationType;
	protected Label lblContacter;
	protected Label lblRelationTime;
	protected Label lblAnnalContent;
	protected Label lblInputer;
	protected PlaceHolder phCheck;
	protected HiddenField hfClientID;
	protected HiddenField hfContacterID;
	protected HiddenField hfClientName;
	protected TextBox txtClientName;
	protected HtmlAnchor showClient;
	protected DropDownList ddlRelationType;
	protected RequiredFieldValidator rfv;
	protected DropDownList ddlContacter;
	protected TextBox txtRelationTime;
	protected TextBox txtAnnalContent;
	protected HtmlInputCheckBox ckbNextAnnal;
	protected HtmlGenericControl nextTiXing;
	protected UpdatePanel UpdatePanel1;
	protected DropDownList ddlRemindRelationType;
	protected CheckBox ckbRemindByEmail;
	protected CheckBox ckbRemindBySms;
	protected TextBox txtRemindTime;
	protected HtmlGenericControl TXTime;
	protected DropDownList ddlRemindTime;
	protected TextBox txtRemindContent;
	protected Button btnSave;
	protected Button btnBack;
	protected PlaceHolder phAddOrEdit;
	public string TabMenu
	{
		get
		{
			if (base.Request.QueryString["TabMenu"] != null)
			{
				this.tabMenu = base.Request.QueryString["TabMenu"].Trim();
			}
			return this.tabMenu;
		}
		set
		{
			this.tabMenu = value;
		}
	}
	public string ActionTab
	{
		get
		{
			if (base.Request.QueryString["action"] != null)
			{
				this.actionTab = base.Request.QueryString["action"].Trim();
			}
			else
			{
				this.actionTab = "";
			}
			return this.actionTab;
		}
		set
		{
			this.actionTab = value;
		}
	}
	public int AnnalID
	{
		get
		{
			if (base.Request.QueryString["annalID"] != null)
			{
				this.annalID = Convert.ToInt32(base.Request.QueryString["annalID"].Trim());
			}
			return this.annalID;
		}
		set
		{
			this.annalID = value;
		}
	}
	public HoWave.Data.Bll.ClientAnnal AnnalBll
	{
		get
		{
			if (this.annalBll == null)
			{
				this.annalBll = new HoWave.Data.Bll.ClientAnnal();
			}
			return this.annalBll;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.OwnerPowerClientRelation(base.user.UserID))
			{
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(this.TabMenu);
			this.BindDropDownList();
			this.BindClientAnnal();
			if (this.AnnalID > 0)
			{
				this.InitPage();
			}
		}
		else
		{
			this.m_flag = Cookie.GetCookie("Contacter_m_flag");
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (!(DisplayName == "tabMnage"))
			{
				if (DisplayName == "tabAdd")
				{
					this.m_flag = "1";
					this.phManage.Visible = false;
					this.phAddOrEdit.Visible = true;
					this.showClient.Attributes.Add("onclick", "SelectKehu('" + this.txtClientName.ClientID + "');");
					if (this.ActionTab == "addinfo")
					{
						this.lblAddOrEdit.Text = "添加联系记录";
					}
					else
					{
						if (this.ActionTab == "editinfo")
						{
							this.lblAddOrEdit.Text = "修改联系记录";
						}
					}
				}
			}
			else
			{
				this.m_flag = "0";
				this.phManage.Visible = true;
				this.phAddOrEdit.Visible = false;
				this.phCheck.Visible = false;
				if (this.ActionTab == "checkinfo")
				{
					this.phManage.Visible = false;
					this.phCheck.Visible = true;
				}
			}
		}
		Cookie.SaveCookie("Contacter_m_flag", this.m_flag, 0, "");
	}
	protected void BindDropDownList()
	{
		this.ddlRelationType.bindDataToDropDownList("HL_ClientAnnal", "RelationType", true, false);
		this.ddlRemindRelationType.bindDataToDropDownList("HL_ClientAnnal", "RelationType", true, false);
		this.ddlSearchRelationType.bindDataToDropDownList("HL_ClientAnnal", "RelationType", false, false);
	}
	private DateTime GetRemindTime()
	{
		DateTime now = DateTime.Now;
		DateTime result = now;
		string selectedValue = this.ddlRemindTime.SelectedValue;
		switch (selectedValue)
		{
		case "tomorrow":
			result = now.AddDays(1.0);
			break;
		case "aftertomorrow":
			result = now.AddDays(2.0);
			break;
		case "threeday":
			result = now.AddDays(3.0);
			break;
		case "week":
			result = now.AddDays(7.0);
			break;
		case "onemonth":
			result = now.AddMonths(1);
			break;
		case "twomonth":
			result = now.AddMonths(2);
			break;
		case "threemonth":
			result = now.AddMonths(3);
			break;
		}
		return result;
	}
	protected HoWave.Data.Model.ClientContacter getContacter(string ccValue)
	{
		HoWave.Data.Model.ClientContacter result = null;
		if (!string.IsNullOrEmpty(ccValue))
		{
			result = HoWave.Data.Bll.ClientContacter.instance.GetModel(Convert.ToInt32(ccValue));
		}
		return result;
	}
	protected HoWave.Data.Model.ClientRemindItem GetClientRemindItem()
	{
		HoWave.Data.Model.ClientRemindItem clientRemindItem = new HoWave.Data.Model.ClientRemindItem();
		string clientName = this.hfClientName.Value.Trim();
		string selectedValue = this.ddlRemindRelationType.SelectedValue;
		string value = this.hfContacterID.Value;
		string text = this.txtRemindContent.Text;
		DateTime value2 = this.GetRemindTime();
		if (this.ddlRemindTime.SelectedValue == "detail")
		{
			value2 = Convert.ToDateTime(this.txtRemindTime.Text);
		}
		HoWave.Data.Model.Client clientByClientName = HoWave.Data.Bll.Client.instance.GetClientByClientName(clientName, base.user.UserID);
		clientRemindItem.RemindByEmail = false;
		clientRemindItem.RemindBySms = false;
		clientRemindItem.RemindByTips = false;
		if (clientByClientName != null)
		{
			HoWave.Data.Model.ClientContacter contacter = this.getContacter(value);
			if (contacter != null)
			{
				clientRemindItem.CorrelativeContacter = contacter.TrueName;
				try
				{
					string smtpserver = this.config["MailSmtpServer"].ToString();
					string mailuser = this.config["MailServerUserName"].ToString();
					string mailpassword = this.config["MailServerPassWord"].ToString();
					string mailFrom = this.config["MailServerFrom"].ToString();
					string text2 = "联系" + contacter.TrueName + "!提醒我见客户";
					if (this.ckbRemindByEmail.Checked)
					{
						clientRemindItem.RemindByEmail = true;
						Email email = new Email();
						string email2 = contacter.Email;
						string mailTitle = contacter.TrueName + "的预约提醒信息";
						email.SendEmail(smtpserver, mailuser, mailpassword, mailFrom, text2, email2, mailTitle);
					}
					if (this.ckbRemindBySms.Checked)
					{
						string mobile = contacter.Mobile;
						clientRemindItem.RemindBySms = true;
						if (!string.IsNullOrEmpty(mobile))
						{
							MobileMessage mobileMessage = new MobileMessage();
							mobileMessage.Send(mobile, text2, contacter.TrueName);
						}
					}
				}
				catch (Exception ex)
				{
					ex.WriteErrorLog(new string[0]);
				}
			}
			clientRemindItem.CorrelativeClient = new int?(clientByClientName.ClientID);
		}
		clientRemindItem.Creater = base.user.UserName;
		clientRemindItem.CreateTime = new DateTime?(DateTime.Now);
		clientRemindItem.RemindTime = new DateTime?(value2);
		clientRemindItem.RelationType = selectedValue;
		clientRemindItem.RemindContent = text;
		clientRemindItem.Status = false;
		return clientRemindItem;
	}
	protected HoWave.Data.Model.ClientAnnal GetClientAnnal()
	{
		HoWave.Data.Model.ClientAnnal clientAnnal = new HoWave.Data.Model.ClientAnnal();
		string clientName = this.hfClientName.Value.Trim();
		string selectedValue = this.ddlRelationType.SelectedValue;
		string value = this.hfContacterID.Value;
		string text = this.txtRelationTime.Text;
		string text2 = this.txtAnnalContent.Text;
		HoWave.Data.Model.Client clientByClientName = HoWave.Data.Bll.Client.instance.GetClientByClientName(clientName, base.user.UserID);
		if (clientByClientName != null)
		{
			HoWave.Data.Model.ClientContacter contacter = this.getContacter(value);
			if (contacter != null)
			{
				clientAnnal.Contacter = contacter.TrueName;
			}
			clientAnnal.ClientID = clientByClientName.ClientID;
			this.hfClientID.Value = clientByClientName.ClientID.ToString();
		}
		clientAnnal.RelationType = selectedValue;
		clientAnnal.RelationTime = new DateTime?(Convert.ToDateTime(text));
		clientAnnal.AnnalTime = new DateTime?(DateTime.Now);
		clientAnnal.AnnalContent = text2;
		clientAnnal.Inputer = base.user.UserName;
		return clientAnnal;
	}
	private bool CheckInput()
	{
		bool result = true;
		if (!string.IsNullOrEmpty(this.txtRelationTime.Text))
		{
			DateTime now = DateTime.Now;
			DateTime t = Convert.ToDateTime(this.txtRelationTime.Text.Trim());
			if (t > now)
			{
				MessageBox.Show(this, "联系时间不能大于当前时间！");
				result = false;
			}
		}
		if (this.ckbNextAnnal.Checked)
		{
			if (this.ddlRemindTime.SelectedValue == "detail")
			{
				if (string.IsNullOrEmpty(this.txtRemindTime.Text.Trim()))
				{
					MessageBox.Show(this, "提醒时间不能为空！");
					result = false;
				}
			}
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.ClientAnnal model = this.AnnalBll.GetModel(this.AnnalID);
		if (model != null)
		{
			HoWave.Data.Model.Client model2 = HoWave.Data.Bll.Client.instance.GetModel(model.ClientID);
			if (model2 != null)
			{
				string clientName = model2.ClientName;
				this.hfClientName.Value = clientName;
				this.txtClientName.Text = clientName;
				this.lblClientName.Text = clientName;
			}
			HoWave.Data.Model.ClientContacter contacterByTrueName = HoWave.Data.Bll.ClientContacter.instance.GetContacterByTrueName(model.Contacter);
			if (contacterByTrueName != null)
			{
				this.lblContacter.Text = model.Contacter;
				this.hfContacterID.Value = contacterByTrueName.ContacterID.ToString();
				this.ddlContacter.ddlSelect(contacterByTrueName.ContacterID.ToString());
			}
			this.ddlRelationType.ddlSelect(model.RelationType);
			this.txtRelationTime.Text = model.RelationTime.ToString();
			this.txtAnnalContent.Text = model.AnnalContent;
			this.lblRelationType.Text = this.GetDictionaryTitle(model.RelationType);
			this.lblRelationTime.Text = model.RelationTime.ToString();
			this.lblAnnalContent.Text = model.AnnalContent;
			this.lblInputer.Text = model.Inputer;
		}
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.Page.IsValid && this.CheckInput())
		{
			HoWave.Data.Model.ClientAnnal clientAnnal = this.GetClientAnnal();
			if (this.ActionTab == "addinfo")
			{
				int num = this.AnnalBll.Add(clientAnnal);
				if (num > 0)
				{
					this.UpdateClientRelationTime();
					if (this.ckbNextAnnal.Checked)
					{
						HoWave.Data.Bll.ClientRemindItem clientRemindItem = new HoWave.Data.Bll.ClientRemindItem();
						HoWave.Data.Model.ClientRemindItem clientRemindItem2 = this.GetClientRemindItem();
						num = clientRemindItem.Add(clientRemindItem2);
					}
					this.WriteSuccessMsg("", "添加联系记录信息成功！", "AnnalManage.aspx?TabMenu=tabMnage");
				}
				else
				{
					this.WriteErrMsg("", "添加联系记录信息失败！", "");
				}
			}
			else
			{
				if (this.ActionTab == "editinfo")
				{
					clientAnnal.ClientAnnalID = this.AnnalID;
					bool flag = this.AnnalBll.Update(clientAnnal);
					if (flag)
					{
						this.UpdateClientRelationTime();
						this.WriteSuccessMsg("", "更新联系记录信息成功！", "AnnalManage.aspx?TabMenu=tabMnage");
					}
					else
					{
						this.WriteErrMsg("", "更新联系记录信息失败！", "");
					}
				}
			}
		}
	}
	private void UpdateClientRelationTime()
	{
		if (!string.IsNullOrEmpty(this.hfClientID.Value))
		{
			HoWave.Data.Model.Client model = HoWave.Data.Bll.Client.instance.GetModel(Convert.ToInt32(this.hfClientID.Value));
			if (model != null)
			{
				model.LastRelationTime = new DateTime?(Convert.ToDateTime(this.txtRelationTime.Text));
				model.ClientID = Convert.ToInt32(this.hfClientID.Value);
				HoWave.Data.Bll.Client.instance.Update(model);
			}
		}
	}
	protected void btnBack_Click(object sender, EventArgs e)
	{
		string url = "AnnalManage.aspx?TabMenu=Myclient" + this.linkHead + "&pageindex";
		base.Response.Redirect(url);
	}
	protected string GetDictionaryTitle(string fieldValue)
	{
		string result = "";
		HoWave.Data.Model.Dictionary dictionary = HoWave.Data.Bll.Dictionary.instance.GetDictionary(fieldValue);
		if (dictionary != null)
		{
			result = dictionary.FieldText;
		}
		return result;
	}
	protected void BindClientAnnal()
	{
		string query = this.GetQuery();
		string queryByOrder = this.GetQueryByOrder();
		int num = this.gvManage.PageIndex;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		string text = " from HL_Client as c,HL_ClientAnnal as ca where ca.ClientID=c.ClientID and c.Owner=" + base.user.UserID + " ";
		int pageSize = this.gvManage.PageSize;
		ClientByPage clientByPage = new ClientByPage();
		int listCount = clientByPage.GetListCount("select count(*)" + text + query);
		int startIndex = (num - 1) * pageSize + 1;
		int endIndex = num * pageSize;
		string str = "ROW_NUMBER() OVER (" + queryByOrder + ") as row,c.ClientName,ca.* ";
		string strSQL = "select " + str + text + query;
		DataTable listPage = clientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
		this.gvManage.DataSource = listPage;
		this.gvManage.DataBind();
		string url = "AnnalManage.aspx?TabMenu=tabMnage" + this.linkHead + "&pageindex";
		this.ltrPagePanel.Text = CurrPageLinks.instance.CurrPageLinkStr(num, pageSize, listCount, "条记录&nbsp;&nbsp;每页&nbsp;" + pageSize + "&nbsp;条记录", url);
	}
	private string GetQuery()
	{
		string text = "";
		string text2 = Fetch.Get("RelationTime");
		if (!string.IsNullOrEmpty(text2))
		{
			this.ddlDate.ddlSelect(text2);
		}
		else
		{
			text2 = this.ddlDate.SelectedValue.Trim();
		}
		if (!string.IsNullOrEmpty(text2))
		{
			DateTime now = DateTime.Now;
			DateTime dateTime = now.AddDays((double)(1 - Convert.ToInt32(now.DayOfWeek.ToString("d"))));
			DateTime dateTime2 = dateTime.AddDays(6.0);
			string text3 = text2;
			if (text3 != null)
			{
				if (!(text3 == "today"))
				{
					if (!(text3 == "week"))
					{
						if (!(text3 == "weeklast"))
						{
							if (!(text3 == "month"))
							{
								if (text3 == "monthlast")
								{
									text += " and (DATEDIFF(MM,RelationTime,GETDATE())=1)";
								}
							}
							else
							{
								text += " and (DATEDIFF(MM,RelationTime,GETDATE())=0)";
							}
						}
						else
						{
							string text4 = text;
							text = string.Concat(new string[]
							{
								text4,
								" and (RelationTime>='",
								dateTime.AddDays(-7.0).ToString("yyyy-MM-dd"),
								"' and RelationTime<='",
								dateTime2.AddDays(-7.0).ToString("yyyy-MM-dd"),
								"')"
							});
						}
					}
					else
					{
						string text4 = text;
						text = string.Concat(new string[]
						{
							text4,
							" and (RelationTime>='",
							dateTime.ToString("yyyy-MM-dd"),
							"' and RelationTime<='",
							dateTime2.ToString("yyyy-MM-dd"),
							"')"
						});
					}
				}
				else
				{
					text += " and (DATEDIFF(DD,RelationTime,GETDATE())=0)";
				}
			}
			this.linkHead = this.linkHead + "&RelationTime=" + text2;
		}
		string text5 = Fetch.Get("BeginTime");
		if (!string.IsNullOrEmpty(text5))
		{
			this.txtBeginTime.Text = text5;
		}
		else
		{
			text5 = this.txtBeginTime.Text;
		}
		if (!string.IsNullOrEmpty(text5))
		{
			text = text + " and DATEDIFF(DD, RelationTime,'" + text5 + "')<=0";
			this.linkHead = this.linkHead + "&BeginTime=" + text5;
		}
		string text6 = Fetch.Get("EndTime");
		if (!string.IsNullOrEmpty(text6))
		{
			this.txtEndTime.Text = text6;
		}
		else
		{
			text6 = this.txtEndTime.Text;
		}
		if (!string.IsNullOrEmpty(text6))
		{
			if (!string.IsNullOrEmpty(text5))
			{
				if (Convert.ToDateTime(text5) > Convert.ToDateTime(text6))
				{
					this.WriteErrMsg("", Language.Get("Common/DateTimeErr"), "");
				}
			}
			text = text + "and DATEDIFF(DD, RelationTime,'" + text6 + "')>=0";
			this.linkHead = this.linkHead + "&EndTime=" + text6;
		}
		string text7 = Fetch.Get("KeHuName");
		if (!string.IsNullOrEmpty(text7))
		{
			this.txtSearchClientName.Text = text7;
		}
		else
		{
			text7 = this.txtSearchClientName.Text;
		}
		if (!string.IsNullOrEmpty(text7))
		{
			text = text + " and c.ClientName like '%" + text7 + "%'";
			this.linkHead = this.linkHead + "&KeHuName=" + text7;
		}
		string text8 = Fetch.Get("RelationType");
		if (!string.IsNullOrEmpty(text8))
		{
			this.ddlSearchRelationType.ddlSelect(text8);
		}
		else
		{
			text8 = this.ddlSearchRelationType.SelectedValue.Trim();
		}
		if (!string.IsNullOrEmpty(text8))
		{
			text = text + " and RelationType='" + text8 + "'";
			this.linkHead = this.linkHead + "&RelationType=" + text8;
		}
		return text;
	}
	private string GetQueryByOrder()
	{
		string result = "order by ca.ClientAnnalID desc";
		string value = this.ddlDate.SelectedValue.Trim();
		if (!string.IsNullOrEmpty(value))
		{
			result = "order by ca.RelationTime desc";
		}
		string value2 = this.txtBeginTime.Text.Trim();
		string value3 = this.txtEndTime.Text.Trim();
		if (!string.IsNullOrEmpty(value2) || !string.IsNullOrEmpty(value3))
		{
			result = "order by ca.RelationTime desc";
		}
		return result;
	}
	protected void gvManage_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected void gvManage_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		this.gvManage.PageIndex = e.NewPageIndex;
		this.BindClientAnnal();
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string commandName = e.CommandName;
		if (commandName == "MoreDelete")
		{
			string text = Text.CheckNumList(Fetch.GetOrPost("annalIdSelect"));
			if (text != "")
			{
				bool flag = false;
				string text2 = text;
				string[] array = text2.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					flag = this.AnnalBll.DeleteList(array[i]);
				}
				if (flag)
				{
					this.WriteErrMsg("", "批量信息删除成功！", "AnnalManage.aspx?TabMenu=tabMnage" + this.linkHead + "&pageindex");
				}
			}
			else
			{
				this.WriteErrMsg("", "请选您要删除的信息", "");
			}
		}
		else
		{
			if (commandName == "CheckInfo")
			{
				string text3 = e.CommandArgument.ToString();
				if (!string.IsNullOrEmpty(text3))
				{
					base.Response.Redirect("AnnalManage.aspx?TabMenu=tabMnage&action=checkinfo&annalID=" + text3);
				}
			}
			else
			{
				if (commandName == "EditInfo")
				{
					string text4 = e.CommandArgument.ToString();
					if (!string.IsNullOrEmpty(text4))
					{
						base.Response.Redirect("AnnalManage.aspx?TabMenu=tabAdd&action=editinfo&annalID=" + text4);
					}
				}
				else
				{
					if (commandName == "DelInfo")
					{
						string text3 = e.CommandArgument.ToString();
						HoWave.Data.Model.ClientAnnal model = this.AnnalBll.GetModel(Convert.ToInt32(text3));
						if (model != null)
						{
							bool flag2 = this.AnnalBll.Delete(Convert.ToInt32(text3));
							if (flag2)
							{
								this.WriteErrMsg("", "删除成功！", "AnnalManage.aspx?TabMenu=tabMnage" + this.linkHead + "&pageindex");
							}
						}
					}
				}
			}
		}
		this.BindClientAnnal();
	}
	protected void SeachBut_Click(object sender, EventArgs e)
	{
		this.m_flag = "0";
		this.BindClientAnnal();
	}
}
